/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.UserBean;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Accessory;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class BusinessFileFormCtr extends SimpleFormController {
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return new Accessory();
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		MultipartFile multipartFile = multipartRequest
				.getFile("accessory_file");
		String sourceFileName = request.getParameter("hid_file_name");
		if (sourceFileName != null && !sourceFileName.equals("")) {
			sourceFileName = sourceFileName.replace("/", "\\");
			String[] tmpArr = sourceFileName.split("\\\\");
			String fileName = tmpArr[tmpArr.length - 1];
			String folder = ub.getUser().getId() + "_"
					+ System.currentTimeMillis();
			String uploadPath = EncCommonHelper.getUploadVirtualPath(this
					.getServletContext());
			String realPath = this.getServletContext().getRealPath(uploadPath)
					+ System.getProperty("file.separator") + folder;
			String virPath = uploadPath + "/" + folder;
			if (multipartFile != null && !multipartFile.isEmpty()
					&& sourceFileName != null && !sourceFileName.equals("")) {
				File file = new File(realPath);
				if (!file.isDirectory()) {
					file.mkdir();
				}

				InputStream in = null;
				FileOutputStream out = null;

				try {
					in = multipartFile.getInputStream();
					out = new FileOutputStream(new File(realPath
							+ System.getProperty("file.separator") + fileName));

					byte[] b;
					for (boolean flag = true; flag; b = (byte[]) null) {
						b = new byte[1024];
						int num = in.read(b);
						if (num > -1) {
							out.write(b);
						} else {
							flag = false;
						}
					}
				} finally {
					try {
						if (in != null) {
							in.close();
						}
					} catch (Exception arg29) {
						;
					}

					try {
						if (out != null) {
							out.close();
						}
					} catch (Exception arg28) {
						;
					}

				}
			}

			mv.addObject("loc_fn", fileName);
			mv.addObject("rem_fn", virPath + "/" + fileName);
			return mv;
		} else {
			errors.rejectValue("fileName", "", "请选择要上传的文件，或输入资料路径");
			return this.showForm(request, errors, this.getFormView());
		}
	}
}